import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
// 导入通用样式
import '@/assets/global.less'
// 导入elementUI
import ElementUI, { Message } from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

// 导入富文本语言
import VueQuillEditor from 'vue-quill-editor'

// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入axios
import axios from 'axios'

Vue.use(VueQuillEditor)
// 设置根路径
axios.defaults.baseURL = 'http://www.liulongbin.top:3008'
Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.prototype.$http = axios
// 配置请求拦截器
axios.interceptors.request.use(function (config) {
  // 为请求头挂载 Authorization 字段
  const token = store.state.user.token
  if (token) {
    config.headers.Authorization = token
  }
  return config
}, function (error) {
  // Do something with request error
  return Promise.reject(error)
})
// 设置响应拦截器
axios.interceptors.response.use(function (response) {
  return response
}, function (error) {
  if (error.response.status === 401) {
    store.state.user.token = ''
    router.push('/login')
    Message.error(error.response.data.message)
  }
  return Promise.reject(error)
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
